package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.*;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.ShopDetailTemplate;
import cn.wolfcode.luowowo.search.template.ShopStrategyTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.shop.domain.ShopDetail;
import cn.wolfcode.luowowo.shop.domain.ShopStrategy;
import cn.wolfcode.luowowo.shop.domain.ShopStrategyTheme;
import cn.wolfcode.luowowo.shop.service.IShopDetailService;
import cn.wolfcode.luowowo.shop.service.IShopStrategyService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Controller
public class DataController {

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private IShopStrategyService shopStrategyService;
    @Reference
    private IShopStrategyTemplateSearchService shopStrategyTemplateSearchService;
    @Reference
    private IShopDetailService shopDetailService;
    @Reference
    private IShopDetailTemplateSearchService shopDetailTemplateSearchService;
    @Reference
    private IDestinationDetailTemplateSearchService destinationDetailTemplateSearchService;

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Model model) {
        //攻略初始化----------------------------------------------------------------------------
        /*List<StrategyDetail> all = strategyDetailService.list();
        for (StrategyDetail detail : all) {
            StrategyTemplate t = new StrategyTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());
            *//*List<String> list = strategyTagService.getTagsBySDetailId(detail.getId());
            t.setTags(list);*//*
            Destination dest = destinationService.getCountry(detail.getDest().getId());

            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            dest = destinationService.getProvince(detail.getDest().getId());
            if (dest != null) {
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }
            strategyTemplateService.saveOrUpdate(t);
        }*/

        //游记初始化----------------------------------------------------------------------------
        /*List<Travel> ts = travelService.list();

        for (Travel t : ts) {
            TravelTemplate tt = new TravelTemplate();
            tt.setId(t.getId());
            tt.setAuthorId(t.getAuthor().getId());
            tt.setAuthorName(t.getAuthor().getNickname());
            tt.setCoverUrl(t.getCoverUrl());
            tt.setCreateTime(t.getCreateTime());
            tt.setDestId(t.getDest().getId());
            tt.setDestName(t.getDest().getName());
            tt.setReplynum(t.getReplynum());
            tt.setSummary(t.getSummary());
            tt.setTitle(t.getTitle());
            tt.setViewnum(t.getViewnum());
            travelTemplateSearchService.saveOrUpdate(tt);
        }*/

        //目的地初始化----------------------------------------------------------------------------
        /*List<Destination> ds = destinationService.list();

        for (Destination d : ds) {

            DestinationTemplate tt = new DestinationTemplate();

            tt.setId(d.getId());
            tt.setCoverUrl(d.getCoverUrl());
            tt.setName(d.getName());
            tt.setInfo(d.getInfo());
            destinationTemplateSearchService.saveOrUpdate(tt);
        }*/

        //用户初始化----------------------------------------------------------------------------
 /*       List<UserInfo> us = userInfoService.list();
        for (UserInfo u : us) {
            UserInfoTemplate tt = new UserInfoTemplate();
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoTemplateSearchService.saveOrUpdate(tt);
        }*/

        //目的地搜索初始化----------------------------------------------------------------------------
       /* List<Destination> ds=destinationService.listDetailAndTheme();
        for (Destination d:ds) {
            DestinationDetailTemplate template = new DestinationDetailTemplate();
            try {
                BeanUtils.copyProperties(template,d);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            if (d.getSubDestination()!=null) {
                template.setMonth(d.getSubDestination().getMonth());
                template.setDays(d.getSubDestination().getDays());
            }
            if (d.getDestinationTheme()!=null){

                template.setThemeId(d.getDestinationTheme().getId());
                template.setThemeName(d.getDestinationTheme().getName());
                template.setTypeId(d.getDestinationTheme().getTypeId());
                template.setTypeName(d.getDestinationTheme().getTypeName());
            }

            destinationDetailTemplateSearchService.saveOrUpdate(template);
        }*/
        //物品攻略初始化----------------------------------------------------------------------------
        List<ShopStrategy> all = shopStrategyService.listByIdIsNull();
        for (ShopStrategy strategy : all) {
            ShopStrategyTemplate t = new ShopStrategyTemplate();

            t.setId(strategy.getId());
            t.setName(strategy.getName());
            t.setEnglish(strategy.getEnglish());
            t.setPinyin(strategy.getPinyin());

            if (strategy.getParent()==null){

            }else {
                t.setParentId(strategy.getParent().getId());
                t.setParentName(strategy.getParent().getName());
            }

            if (strategy.getTheme()==null){

            }else {
                t.setThemeId(strategy.getTheme().getId());
                t.setThemeName(strategy.getTheme().getName());
            }
            t.setIshot(strategy.getIshot());
            t.setInfo(strategy.getInfo());
            t.setCoverUrl(strategy.getCoverUrl());
            t.setDeep(strategy.getDeep());
            shopStrategyTemplateSearchService.saveOrUpdate(t);
        }



        //物品初始化----------------------------------------------------------------------------
        List<ShopDetail> all1 = shopDetailService.list();
        for (ShopDetail detail : all1) {
           ShopDetailTemplate t = new ShopDetailTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setInfo(detail.getInfo());

            t.setStrategyId(detail.getShopStrategy().getId());
            t.setStrategyName(detail.getShopStrategy().getName());

            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
;
            t.setUserId(detail.getAuthor().getId());
            t.setUserName(detail.getAuthor().getNickname());

            t.setCoverUrl(detail.getCoverUrl());

            t.setRef(detail.getRef());
            t.setIntegral(detail.getIntegral());
            t.setPrice(detail.getPrice());

            t.setCreateTime(detail.getCreateTime());

            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());
            t.setSellsun(detail.getSellsun());




            //还没有用到
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());


            shopDetailTemplateSearchService.saveOrUpdate(t);

        }
        return "好的";
    }
}
